package com.xinxing.learning.batch.increment.step;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinxing.learning.batch.entity.User;
import com.xinxing.learning.batch.mapper.target.TargetUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
public class IncrementUserItemWriter implements ItemWriter<User> {

    @Autowired
    private TargetUserMapper targetUserMapper;

    @Override
    public void write(List<? extends User> list) throws Exception {
        for (User user : list) {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId, user.getId());
            boolean exists = targetUserMapper.exists(userLambdaQueryWrapper);
            if (exists) {
                log.info("存在");
                targetUserMapper.updateById(user);
            } else {
                log.info("不存在");
                int i = 1 / 0;
                targetUserMapper.insert(user);
            }
        }
    }
}
